# -*- coding:utf-8 -*-
from lxml import etree
def getxpath(html):
    return etree.HTML(html)


sample1 = """<html>
  <head>
    <title>My page</title>
  </head>
  <body>
    <h2>Welcome to my <a href="#" src="x">page</a></h2>

    <p>This is the first paragraph.</p>
     <h2>Hello xpath </h2>
    <!-- this is the end -->
  </body>
</html>
"""

s1=getxpath(sample1)
print s1.xpath('//title/text()')
print s1.xpath('/html/head/title/text()')
print s1.xpath('//h2/a/@href')
print s1.xpath('//h2/a/text()')
print s1.xpath('//text()')
print s1.xpath('//h2/a/text()')

sample2 = """
<html>
  <body>
    <ul>
      <li>Quote 1</li>
      <li>Quote 2 with <a href="...">link</a></li>
      <li>Quote 3 with <a href="...">another link</a></li>
      <li><h2>Quote 4 title</h2> ...</li>
       <li><h3>I love h3</h3> This is a test</li>
    </ul>
  </body>
</html>
"""
s2 = getxpath(sample2)
print s2.xpath('//li/text()')
# print s2.xpath('//li/h2/text()')
# print s2.xpath('//li[position() mod2=1]/text()')
# print s2.xpath('//li[h2 | h3]/text()')

sample3 = """<html>
  <body>
    <ul>
      <li id="begin"><a href="https://scrapy.org">Scrapy</a>Ai xin</li>
      <li><a href="https://scrapinghub.com">Scrapinghub</a></li>
      <li><a href="https://blog.scrapinghub.com">Scrapinghub Blog</a></li>
      <li id="end"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
      <li data-xxxx="end" abc="abc"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
    </ul>
  </body>
</html>
"""
s3 = etree.HTML(sample3)
# print s3.xpath('//li/a[@href="https://scrapy.org"]/text()')
# print s3.xpath('//li[@id="begin"]/text()')
# print s3.xpath('//li/a[text()="Scrapinghub Blog"]/text()')
# print s3.xpath('//li[@data-xxxx]/text()')

sample4 = u"""
<html>
  <head>
    <title>My page</title>
  </head>
  <body>
    <h2>Welcome to my <a href="#" src="x">page</a></h2>
    <p>This is the first paragraph.</p>
    <p class="test">
    编程语言<a href="#">python</a>
    <img src="#" alt="test"/>javascript
    <a href="#"><strong>C#</strong>JAVA</a>
    </p>
     <p class="$">123<a>来获取我啊</a></p>
    <p class="content-a">a</p>
    <p class="content-b">b</p>
    <p class="content-c">c</p>
    <p class="content-d">d</p>
    <p class="econtent-e">e</p>
    <p class="heh">f</p>
    <!-- this is the end -->
  </body>
</html>
"""
s4=etree.HTML(sample4)
print s4.xpath('//p[@class]/text()')
print ''.join(s4.xpath('//p[@class="test"]/text()'))
print s4.xpath('//p[@class="test"]/text()')
print  s4.xpath('string(//p[@class="test"])')
print  s4.xpath('string(//p[@class="test"])')
print s4.xpath('//p[contains(@class,"content")]/text()')
print s4.xpath('string(//p[@class="$"])')
print s4.xpath('//p[@class="$"]/text()')
print s4.xpath('//p/@class')